package com.syb.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.syb.dao.RelResourcesRoleDao;
import com.syb.dao.ResourcesDao;
import com.syb.entity.function.SysResource;
import com.syb.service.ResourceService;
import com.syb.util.PageResults;

@Service
public class ResourceServiceImpl implements ResourceService {

	@Autowired
	private ResourcesDao resourceDao;
	
	@Autowired
	private RelResourcesRoleDao relResoucesRoleDao;
	
	@Override
	public PageResults<SysResource> findSysResourcesList(PageResults<SysResource> page) throws Exception {
		PageResults<SysResource> pageList = new PageResults<SysResource>();
		List<SysResource> resourceList  = new ArrayList<SysResource>();
		int count = 0 ;
		try{
			resourceList = resourceDao.findResourcesList(page);
		}catch(Exception e){
			throw new Exception(e.getMessage());
		}
		try{
			count = resourceDao.findResourcesCount(page);
		}catch(Exception e){
			throw new Exception(e.getMessage());
		}
		pageList.setResults(resourceList);
		pageList.setTotalCount(count);
		pageList.setPageNo((page.getCurrentPage()/page.getPageSize())+1);
		return pageList;
	}

	@Override
	public List<SysResource> findResourcesDing() throws Exception {
		List<SysResource> sysResource = new ArrayList<SysResource>();
		try{
			sysResource =  resourceDao.findResorcesDing();
		}catch(Exception e){
			throw new Exception(e.getMessage());
		}
		return sysResource;
	}

	@Override
	public void insertResource(SysResource sys) throws Exception {
		//根据名称查询是否已存在
		List<SysResource> sysList = new ArrayList<SysResource>();
		SysResource sysName = new SysResource();
		sysName.setResourceName(sys.getResourceName());
		try{
			sysList = resourceDao.findResourceByName(sysName);
		}catch(Exception e){
			throw new Exception(e.getMessage());
		}
		if(sysList!=null && sysList.size()>0){
			throw new Exception("资源名称已存在！");
		}
		try{
			resourceDao.insertResource(sys);
		}catch(Exception e){
			throw new Exception(e.getMessage());
		}
	}

	@Override
	public void delResource(SysResource sys) throws Exception {
		//删除资源同时要删除角色和资源表
		try{
			relResoucesRoleDao.delRelResoucesRole(sys.getId());
		}catch(Exception e){
			throw new Exception(e.getMessage());
		}
		try{
			resourceDao.delResource(sys);
			}catch(Exception e){
				throw new Exception(e.getMessage());
			}
	}

	@Override
	public void updateResource(SysResource sys) throws Exception {
		//根据id查询更新的数据是否存在
		List<SysResource> sysList = new ArrayList<SysResource>();
		SysResource sysRes = new SysResource();
		sysRes.setId(sys.getId());
		try{
		sysList = resourceDao.findResourceByName(sysRes);
		}catch(Exception e){
			throw new Exception("根据id查询资源失败！");
		}
		if(sysList.size()<=0){
			throw new Exception("更新的资源不存在！");
		}
		if(!sys.getResourceName().equals(sysList.get(0).getResourceName())){
			SysResource sysRes2 = new SysResource();
			sysRes2.setResourceName(sys.getResourceName());
			try{
				sysList = resourceDao.findResourceByName(sysRes2);
				}catch(Exception e){
					throw new Exception("根据资源名称查询资源失败！");
				}
			if(sysList.size()>0){
				throw new Exception("资源名称已存在！");
			}
		}
		
		try{
			resourceDao.updateResource(sys);
			}catch(Exception e){
				throw new Exception(e.getMessage());
			}
	}

	@Override
	public List<SysResource> findResourceByName(SysResource name) throws Exception {
		List<SysResource> sys = new ArrayList<SysResource>();
		try{
			sys = resourceDao.findResourceByName(name);
		}catch(Exception e){
			throw new Exception(e.getMessage());
		}
		return sys;
	}

}
